//
//  BBSystemSettingViewController.m
//  BlockBrowser
//
//  Created by Mac on 2018/4/8.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBSystemSettingViewController.h"
#import "NSBundle+STSystemTool.h"
#import "BBClearPresentViewController.h"
@interface BBSystemSettingViewController ()
@property(nonatomic, strong) UISwitch                     *openLastSwitch;//是否打开上次页面
@property(nonatomic, strong) UISwitch                     *pageSwitch;//翻页方式
@property(nonatomic, strong) NSArray                     *dataSouce;
@end

@implementation BBSystemSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"系统设置";
    [self configDataSouce];
    // Do any additional setup after loading the view.
}

- (void)configDataSouce{
    self.openLastSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
    [self.openLastSwitch setOnTintColor:TM_ThemeBackGroundColor];
    self.pageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
    [self.pageSwitch setOnTintColor:TM_ThemeBackGroundColor];
    
    BOOL openLast = [[NSUserDefaults standardUserDefaults] boolForKey:openLastKey];
    BOOL page = [[NSUserDefaults standardUserDefaults] boolForKey:pageKey];
    
    [self.openLastSwitch setOn:openLast];
    [self.pageSwitch setOn:page];
    
    [self.openLastSwitch addTarget:self action:@selector(swicthDidChange:) forControlEvents:UIControlEventValueChanged];
    [self.pageSwitch addTarget:self action:@selector(swicthDidChange:) forControlEvents:UIControlEventValueChanged];
    
    NSString *version = [NSString stringWithFormat:@"%@%@",@"v",[NSBundle st_applictionVersin]];
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"清除数据"
                                                       detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"启动时打开上次页面"
                                                       accessoryView:self.openLastSwitch],
                       [[STTableEasyModel alloc] initWithTextString:@"打开点击按钮翻页"
                                                       accessoryView:self.pageSwitch],
//                       [[STTableEasyModel alloc] initWithTextString:@"检查更新"
//                                                       detailString:version],
                       
                       ];
    [self.tableView reloadData];

}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    [cell setSt_tableEasyModel:model];
    return cell;
}

#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    if ([model.textString isEqualToString:@"检查更新"]) {
        [self onSelctedCheckCell];
    }
    if ([model.textString isEqualToString:@"清除数据"]) {
        BBClearPresentViewController * vc = [BBClearPresentViewController new];
        [vc setChosedMenuHandle:^(NSArray *array) {
            if ([array containsObject:@"搜索历史"]) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:searchHistoryListKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
            if ([array containsObject:@"浏览历史"]) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:scanHistoryListKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
            if ([array containsObject:@"收藏历史"]) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:collectionHistoryListKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
            if ([array containsObject:@"表单密码"]) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:loginAccountPwdKey];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:loginAccountPwdKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
            [SVProgressHUD showSuccessWithStatus:@"清除成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
        [self presentViewController:vc animated:NO completion:nil];
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)swicthDidChange:(UISwitch*)sender{
    if (sender == self.openLastSwitch) {
        [[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:openLastKey];
    }
    if (sender == self.pageSwitch) {
        [[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:pageKey];
    }
    
    [SVProgressHUD showSuccessWithStatus:@"修改成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popToRootViewControllerAnimated:YES];
    });
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)onSelctedCheckCell{
    [SVProgressHUD showInfoWithStatus:@"已经是最新版本"];
    return;
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/getInviteInfo"];
    [dic setObject:@"1" forKey:@"type"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 CGFloat  newversion =  [responseObject[@"version"][@"version"] floatValue];
                                                 CGFloat  currentersion = [NSBundle st_applictionVersin].floatValue;
                                                 if (newversion > currentersion) {
                                                     [self showAlertCancelAndConfirm:[NSString stringWithFormat:@"检测到新版本%f，是否更新到最新版本",newversion]
                                                                        andWithBlock:^(NSString *name) {
                                                                            if ([name isEqualToString:@"确认"]) {
                                                                                [self gotoUpdate];
                                                                            }
                                                                        }];
                                                 }else{
                                                     [SVProgressHUD showInfoWithStatus:@"已经是最新版本"];
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)gotoUpdate{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@""]];
}
@end
